<?php

class CrawlerController extends Controller
{
	private $approve = false;
   public function actionReceiveNews(){
      $new_title = Yii::app()->request->getParam('new_title');
      $new_title_md5    = Yii::app()->request->getParam('new_title_md5');
      $new_intro        = Yii::app()->request->getParam('new_intro');
      $new_cat_id       = Yii::app()->request->getParam('new_cat_id');
      $new_avatar       = Yii::app()->request->getParam('new_avatar');
      $new_description  = Yii::app()->request->getParam('new_description');
      $code2            = md5($new_title . "sondv_chinhvt" . date("z") . $new_cat_id);
      $new_web_id          = Yii::app()->request->getParam('new_web_id');
      $new_link_detail     = Yii::app()->request->getParam('new_link_detail');
      $new_link_detail_md5 = Yii::app()->request->getParam('new_link_detail_md5');
      $new_time            = Yii::app()->request->getParam('new_time');
      
      
      $code             = Yii::app()->request->getParam('code');
      $new_description  = base64_decode($new_description);
      
      if($code == $code2){
         echo "ok roi";
         
         echo "<br/>";
         
         /*  
         $tableInsert   = "news_crawler";
         $queryInsert   = "INSERT IGNORE INTO " . $tableInsert . "(title,title_md5,avatar,intro,cat_id,
                           description,web_id,link_detail,link_detail_md5,time) 
                           VALUES('" . $new_title . "','" . $new_title_md5 . "','" . $new_avatar . "','" . $new_intro . "','" . $new_cat_id . "',
                           '" . $new_description . "','" . intval($new_web_id) . "','" . $new_link_detail . "','" . $new_link_detail_md5 . "','" . intval($new_time) . "') ";
         
         //echo $queryInsert;die();
         $db = Yii::app()->db;
         $cmd = $db->createCommand($queryInsert);
         $cmd->execute();
         */
         
         // Them vao bang cho duyet
         
         //Kiem tra tin co bi trung khong
         
         $check_exists = OnlineNews::model()->findByAttributes(array('title_md5' => $new_title_md5));
         if($check_exists == null){
            $online_news = new OnlineNews();
            $online_news->title = $new_title;
            $online_news->title_md5 = $new_title_md5; 
            $online_news->avatar = $new_avatar;
            $online_news->summary = $new_intro;
            $online_news->online_news_category_id = $new_cat_id;
            $online_news->content = $new_description;
            $online_news->web_id = $new_web_id;
            $online_news->vendor_link = $new_link_detail;
            $online_news->vendor_link_md5 = $new_link_detail_md5;
            $online_news->time = $new_time;
            if(!$this->approve){
               $online_news->sync = 1;
            }
            if($online_news->validate()){
               $online_news->save();
               News::model()->makeThumbnail($new_avatar,array('90')); // Tao 1 anh thumbnail
            }   
         }else{
            echo "Trùng tin";
         }
         
         
         if(!$this->approve){ // Nếu tin tức không cần duyệt thì insert trực tiếp vào bảng news
            
            $check_exists = News::model()->findByAttributes(array('title' => $new_title));
            if($check_exists == null){
               $news = new News();
               $news->title = $new_title;
               $news->image = $new_avatar;
               $news->cat_id = $new_cat_id;
               $news->sapo = $new_intro;
               $news->content = $new_description;
               $news->active = 1;
               $news->hot = 0;
               $news->create_date = date('Y-m-d H:i:s',$new_time);
               if($news->validate()){
                  $news->save();
                  News::model()->makeThumbnail($new_avatar); // Tao 3 anh thumbnail
               }else{
                  CVarDumper::dump($news->errors,10,true);
               }   
            }else{
               echo  "Trùng tin";
            }
            
         }
               
      }else{
         echo "da sai";
      }
	}
   
   public function actionReceiveImage(){
      
      
      $pic_id     = Yii::app()->request->getParam('pic_id');
      $pic_name   = Yii::app()->request->getParam('pic_name');
      $pic_data   = Yii::app()->request->getParam('pic_data');
      $pic_path   = Yii::app()->request->getParam('pic_path');
      $code       = Yii::app()->request->getParam('code');
      
      $pic_path1  = Yii::getPathOfAlias('webroot')."/" . $pic_path;
      $pic_path2  = "/" . str_replace($pic_name,"",$pic_path);
      
      echo "<br/>";
      echo $pic_path2;
      echo "<br/>";
      echo $pic_id;
      echo "<br/>";
      $code2   = md5($pic_name . "sondv_chinhvt" . date("z") . $pic_id);
      if($code == $code2){
         echo "ok roi";
         echo "<br/>";
         $pic_path2 = Yii::getPathOfAlias('webroot').str_replace("../pictures/","/pictures/",$pic_path2);
         if(!is_dir($pic_path2)){
            mkdir($pic_path2,0777, true);
            chmod($pic_path2, 0777);
         }   
         if (move_uploaded_file($_FILES["pic_data"]["tmp_name"], $pic_path1)) {
              echo "The file ". basename( $_FILES["pic_data"]["name"]). " has been uploaded.";
          } else {
              echo "Sorry, there was an error uploading your file.";
          }   
         
      }else{
         echo "da sai";
      }
   }
   
   public function actionGetPathImage(){
      $file_name = "xdd1426259638.jpg";
      News::model()->makeThumbnail($file_name);
   }
   
   function getImageFullSize($imgName){
      $imgServer  = "/";
      return $imgServer . 'pictures/picfullsizes/'.$this->getPathDateTimeImg($imgName);
   }
   function getImageSmall($sourceFilename, $maxWidth = 0,$maxHeight = 0){
    $imgServer  = "/";
    global $pathThemes;
    if($maxHeight == 0){
     $maxHeight = 1000;
    }
    if($maxWidth < 50) $maxWidth = 50;
    if($maxHeight < 100&&$maxHeight>0) $maxHeight = 100;
    
    if($sourceFilename == "") return $pathThemes . "/images/default.png";
    return  $imgServer. "pictures/picsmalls/" . getPathDateTimeImg($sourceFilename,"",$maxWidth);
   }
   
   
   function getPathDateTimeImg($filename, $pre_path = "",$width_height =""){
      $explode = explode("/", $filename);
      $filename = end($explode);
      
      $time = intval(preg_replace("/[^0-9]/i","",$filename));
      if($time > time()) $time = time();
      return $pre_path . date("Y/m/d/", $time) . (($width_height != "") ? $width_height . '/' : '') . $filename; 
   }
   
   // Uncomment the following methods and override them if needed
	/*
	public function filters()
	{
		// return the filter configuration for this controller, e.g.:
		return array(
			'inlineFilterName',
			array(
				'class'=>'path.to.FilterClass',
				'propertyName'=>'propertyValue',
			),
		);
	}

	public function actions()
	{
		// return external action classes, e.g.:
		return array(
			'action1'=>'path.to.ActionClass',
			'action2'=>array(
				'class'=>'path.to.AnotherActionClass',
				'propertyName'=>'propertyValue',
			),
		);
	}
	*/
}